package com.hj.ipproxy.service;

import java.util.ArrayList;
import java.util.List;

import com.hj.ipproxy.bean.HttpEntity;
import com.hj.ipproxy.httpclient.TcpType;
import com.hj.tools.FileUtil;
import com.hj.tools.StringUtil;

public class MainRun {
	
	public static void main(String[] args) {
		final ProxyIpService proxSev = ProxyIpServiceImpl.getInstance();
		//HttpEntity ht = proxSev.checkProxyIp("180.177.131.165", 8088);
		//http://opendata.baidu.com/api.php?query=61.141.159.144&co=&resource_id=6006&t=1416634016856&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu
		//Runtime.getRuntime().exec("cmd /c start http://www.163.com");
		
		/*String ip = "180.177.131.165";
		String url = "http://opendata.baidu.com/api.php?query="+ip+"&co=&resource_id=6006&t=1416634016856&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu";
		HttpEntity ht = new HttpEntity(url,ip,8088);
		ht.setTcpType(TcpType.GET_HTTP_PROXY);
		ht.setCharset("gb2312");
		ht.send();
		System.out.println(ht.getBody());*/
		
		/*HttpEntity ht = new HttpEntity("http://www.2345.com/?k520tx");
		ht.setUserAgent(UserAgent.Safari_5_1_Windows.get());
		ht.setCharset("gb2312");
		ht.send();
		System.out.println(ht.getUserAgent());*/
		
		/*Set<ProxyIp> ips = proxSev.getProxyIps();
		System.out.println("有效IP数："+ips.size());
		StringBuilder ipsbd = new StringBuilder();
		for (ProxyIp pi : ips) {
			System.out.println(
					pi.getIp()+"---"+pi.getPort()+"---"+pi.getType()
					+"---"+pi.getAddrs()+"---"+pi.getRespTime());
			ipsbd.append(pi.getIp()).append(":").append(pi.getPort())
				 .append(ProxyIpUtil.NEW_LINE);
		}
		FileUtil.createAndWriteFile("E:\\IP获取\\可用IP.txt", ipsbd.toString());*/
		
		//保存IP到本地硬盘
		/*String s = "58.255.51.74:80"+ProxyIpUtil.NEW_LINE+"58.253.238.242:80"+ProxyIpUtil.NEW_LINE+"113.105.142.228:80";
		System.out.println(s);
		FileUtil.createAndWriteFile("d:/ip.txt", s);*/
		List<String> iplist = FileUtil.getTextFileContent("E:\\IP获取\\可用IP.txt");
		//String strURL = "https://kyfw.12306.cn/otn/leftTicket/queryT?leftTicketDTO.train_date=2014-10-05&leftTicketDTO.from_station=ZZQ&leftTicketDTO.to_station=SZQ&purpose_codes=ADULT";
		String url = "https://kyfw.12306.cn/otn/leftTicket/queryT?leftTicketDTO.train_date=2015-02-02&leftTicketDTO.from_station=IOQ&leftTicketDTO.to_station=ZZQ&purpose_codes=ADULT";
		System.out.println("IP数: "+ iplist.size());
		List<String> ipsv = new ArrayList<String>();
		for (String str : iplist) {
			try {
				String[] ips = str.split(":");
				String ip = ips[0];
				String port = ips[1];
				System.out.println("设置"+ip+":"+port);
				HttpEntity hts = new HttpEntity(url, ip, Integer.parseInt(port));
				hts.setTcpType(TcpType.GET_HTTPS_PROXY);
				//hts.setTcpType(TcpType.GET_HTTPS);
				hts.send();
				if (StringUtil.isNotEmpty(hts.getBody())) {
					System.out.println( hts.getBody());
					ipsv.add(ip+":"+port);
				}
			} catch (Exception e) {
				System.out.println("******************************");
			}
		}
		
		for (String ip : ipsv) {
			System.out.println(ip);
		}
		
	}
	
}
